<template>
  <PageWrapper contentClass="flex" contentFullHeight dense fixedHeight>
    <DeptTree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <div class="w-3/4 xl:w-4/5">
      <BasicTable :searchInfo="searchInfo" class="m-4 ml-2" @register="registerTable">
        <template #toolbar>
          <a-button type="primary" @click="handleCreate">新增</a-button>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <TableAction
              :actions="[
                {
                  icon: 'clarity:note-edit-line',
                  tooltip: '编辑信息',
                  onClick: handleEdit.bind(null, record),
                },
                {
                  icon: 'ant-design:delete-outlined',
                  color: 'error',
                  tooltip: '删除信息',
                  popConfirm: {
                    title: '是否确认删除',
                    placement: 'left',
                    confirm: handleDelete.bind(null, record.id),
                  },
                },
              ]"
            />
          </template>
        </template>
      </BasicTable>
    </div>
    <ModalAdd @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';

  import { BasicTable, TableAction, useTable } from '@/components/Table';
  import { PageWrapper } from '@/components/Page';
  import DeptTree from './DeptTree.vue';

  import { useModal } from '@/components/Modal';

  import { ColumnPage, SearchForm } from './data';
  import { doHelpDelete, getHelpPage } from '@/modules/shop/api';
  import ModalAdd from '@/modules/shop/help/ModalAdd.vue';
  import { useMessage } from '@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  //defineOptions({ name: 'AccountManagement' });

  const searchInfo = reactive<Recordable>({});
  const [registerTable, { reload, updateTableDataRecord, deleteTableDataRecord }] = useTable({
    title: '帮助列表',
    api: getHelpPage,
    rowKey: 'id',
    columns: ColumnPage,
    formConfig: {
      labelWidth: 90,
      schemas: SearchForm,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    handleSearchInfoFn(info) {
      return info;
    },
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
  });

  //add
  const [registerModal, { openModal }] = useModal();

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  //edit
  function handleEdit(record: Recordable) {
    console.log(record);
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  //delete
  function handleDelete(recordId: number) {
    doHelpDelete({ id: recordId }).then((res) => {
      if (res.code > 0) {
        createMessage.warning(res.message);
        return;
      }
      createMessage.success(res.message);
      deleteTableDataRecord(recordId);
    });
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      // 演示不刷新表格直接更新内部数据。
      // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
      const result = updateTableDataRecord(values.id, values);
      console.log(result);
    } else {
      reload();
    }
  }

  function handleSelect(cateId = '') {
    searchInfo.cateId = cateId;
    reload();
  }
</script>
